﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.ZNH.ZMCM.UI.Utils;
using Baosight.Platform.Core.ServiceModel;
using System.Collections.Generic;


namespace Baosight.ZNH.ZMCM.UI.ChildWindows
{
    public partial class ChangeNeedforYearPlanWindow : EFViewCmes
    {
        public ChangeNeedforYearPlanWindow(String _dialogType)
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
            Init(_dialogType);
        }

        /// <summary>
        /// 初始化父类EFViewCmesProperty的值，每次点击“确定”、“返回”时执行此方法，从新赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.ChangeplanWindows.GetChildObjects<EFDataField>(null, true);
            base.ViewProperty = _view;
        }
        /// <summary>
        /// 初始化页面数据
        /// </summary>
        /// <param name="type"></param>
        private void Init(string type)
        {
            type = type == null ? "insert" : type.ToLower();

            switch (type)
            {
                case "insert":
                    
                    base.Mode = OperationMode.Insert;
                    break;
                case "update":
                    base.Mode = OperationMode.Update;
                    break;
                default:
                    break;
            }
        }

        #region   确定/返回按钮事件

        private void Commit_Click(object sender, RoutedEventArgs e)
        {
            Button _button = sender as Button;
            _button.IsEnabled = false;
            base.TempButton = _button;
            String msg = "1";
            IList<EIRow> _list = this.ChildGrid.GetCheckedEIRows() == null ? new List<EIRow>() : this.ChildGrid.GetCheckedEIRows();
            if (_list.Count <= 0)
            {
                msg = "请选择一条或多条明细进行操作！";
            }
            //else
            //{
            //    for (int i = 0; i < _list.Count; i++)
            //    {
            //        double fdPurchaseNum = Convert.ToDouble(this.ChildGrid.GetCheckedEIRows()[i]["fdPurchaseNum"]);
            //        double fdNeedNum = Convert.ToDouble(this.ChildGrid.GetCheckedEIRows()[i]["fdNeedNum"]);
            //        if (fdPurchaseNum > fdNeedNum)
            //        {
            //                msg = "采购数量不能大于需求数量！";
            //                break;
            //        }
            //        if (fdPurchaseNum > fdNeedNum)
            //        {
            //            msg = "采购数量不能大于需求数量！";
            //            break;
            //        }
            //    }
            //}
            if (msg == "1")
            {
                base.TempButton = sender as Button;
                base.TempButton.IsEnabled = false;
                base.Submit(OperationMode.Insert, "ZMCMD4", "insert", DataRange.CheckedItems, DoInsertOperationCompleted, null);
            }
            else
            {
                this.OpenEFWindowAlert(msg, true);
                return;
            }

            //原来的代码
            //EIInfo eiInfo = new EIInfo();
            //int count = this.ChildGrid.CheckedEIRows.Count();
            //if (count <1)
            //{
            //    base.OpenEFWindowAlert("请选择一条或多条明细进行操作", true);
            //}
            //else
            //{
            //    base.TempButton = sender as Button;
            //    base.TempButton.IsEnabled = false;
            //    base.Submit(OperationMode.Insert, "ZMCMD4", "insert", DataRange.CheckedItems, DoInsertOperationCompleted, null);
            //}
        }
        /// <summary>
        /// 新增操作成功后回调
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DoInsertOperationCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            EIInfo eIInfo = e.EIInfo;
            if (eIInfo.Status == -1)
            {
                this.TempButtonOpen(true);
                base.OnCommitMessagePopup(eIInfo);//操作失败，消息提醒
            }
            else
            {
                //操作成功关闭新增页面
                //此处可以扩展是否连续新增
                EFWindow window = this.Parent as EFWindow;
                window.DialogResult = true;
                window.Close();
            }
        }
        private void Calcel_Click(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }

        #endregion
    }
}
